package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import net.maku.events.entity.ActivityQuestionBankOptionEntity;
import net.maku.events.service.ActivityQuestionBankOptionService;
import net.maku.events.vo.ActivityQuestionBankOptionVO;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.convert.ActivityQuestionBankConvert;
import net.maku.events.entity.ActivityQuestionBankEntity;
import net.maku.events.query.ActivityQuestionBankQuery;
import net.maku.events.vo.ActivityQuestionBankVO;
import net.maku.events.dao.ActivityQuestionBankDao;
import net.maku.events.service.ActivityQuestionBankService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 题库题目
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class ActivityQuestionBankServiceImpl extends BaseServiceImpl<ActivityQuestionBankDao, ActivityQuestionBankEntity> implements ActivityQuestionBankService {

    private final ActivityQuestionBankOptionService activityQuestionBankOptionService;

    @Override
    public PageResult<ActivityQuestionBankVO> page(ActivityQuestionBankQuery query) {
        IPage<ActivityQuestionBankEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(ActivityQuestionBankConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<ActivityQuestionBankEntity> getWrapper(ActivityQuestionBankQuery query) {
        LambdaQueryWrapper<ActivityQuestionBankEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ActivityQuestionBankVO vo) {
        ActivityQuestionBankEntity entity = ActivityQuestionBankConvert.INSTANCE.convert(vo);
        baseMapper.insert(entity);
        shareInsert(entity.getId(), vo.getOptions());
    }

    /**
     * 共用
     *
     * @param id      主键
     * @param options 选项
     */
    private void shareInsert(Long id, List<ActivityQuestionBankOptionVO> options) {
        if (CollectionUtils.isNotEmpty(options)) {
            List<ActivityQuestionBankOptionEntity> entityList = BeanUtil.copyToList(options, ActivityQuestionBankOptionEntity.class);
            entityList.forEach(item -> item.setQuestionId(id));
            activityQuestionBankOptionService.saveBatch(entityList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ActivityQuestionBankVO vo) {
        ActivityQuestionBankEntity entity = ActivityQuestionBankConvert.INSTANCE.convert(vo);
        updateById(entity);
        activityQuestionBankOptionService.remove(new LambdaQueryWrapper<ActivityQuestionBankOptionEntity>().eq(ActivityQuestionBankOptionEntity::getQuestionId, entity.getId()));
        shareInsert(entity.getId(), vo.getOptions());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDataById(List<Long> idList) {
        removeByIds(idList);
        activityQuestionBankOptionService.remove(new LambdaQueryWrapper<ActivityQuestionBankOptionEntity>().in(ActivityQuestionBankOptionEntity::getId, idList));
    }

}